package com.cade.agriculturemall.base;

import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.os.IBinder;
import android.os.PersistableBundle;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import com.cade.agriculturemall.R;

/**
 * Created by Lobby on 2017/11/7.
 */

public abstract class FWActivity<P extends IBasePresenter> extends AppCompatActivity implements IBaseView {
    private boolean autoCloseKeyboard = true;

    protected P mPresenter;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) {
        super.onCreate(savedInstanceState, persistentState);
    }

    public abstract void setupView();

    public abstract void setupEvent();

    public abstract void setupData();

    public abstract P setupPresenter();

    @Override
    public void setContentView(@LayoutRes int layoutResID) {
        mPresenter = setupPresenter();
        super.setContentView(layoutResID);
        setupView();
        setupEvent();
        setupData();
    }

    @Override
    public void setContentView(View view) {
        super.setContentView(view);
        mPresenter = setupPresenter();
        setupView();
        setupEvent();
        setupData();
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (mPresenter != null) {
            mPresenter.onResume();
        }
    }

    @Override
    public boolean dispatchTouchEvent(@NonNull MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN &&
                getCurrentFocus() != null &&
                getCurrentFocus().getWindowToken() != null && autoCloseKeyboard) {

            View v = getCurrentFocus();
            if (isShouldHideKeyboard(v, event)) {
                hideKeyboard(v.getWindowToken());
            }
        }
        return super.dispatchTouchEvent(event);
    }


    /**
     * 是否禁止旋转屏幕
     **/
    boolean isAllowScreenRoate = false;

    private void allowScreenRoate() {
        if (!isAllowScreenRoate) {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        } else {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }
    }

    /**
     * 根据EditText所在坐标和用户点击的坐标相对比，来判断是否隐藏键盘，因为当用户点击EditText时则不能隐藏
     */
    private boolean isShouldHideKeyboard(@Nullable View v, @NonNull MotionEvent event) {
        if (v != null && (v instanceof EditText)) {
            int[] l = {0, 0};
            v.getLocationOnScreen(l);
            int left = l[0],
                    top = l[1],
                    bottom = top + v.getHeight(),
                    right = left + v.getWidth();
            return !(event.getRawX() > left && event.getRawX() < right
                    && event.getRawY() > top && event.getRawY() < bottom);
        }
        // 如果焦点不是EditText则忽略，这个发生在视图刚绘制完，第一个焦点不在EditText上，和用户用轨迹球选择其他的焦点
        return false;
    }

    protected void hideKeyboard(@NonNull View view) {
        hideKeyboard(view.getWindowToken());
    }

    /**
     * 获取InputMethodManager，隐藏软键盘
     */
    private void hideKeyboard(@Nullable IBinder token) {
        if (token != null) {
            InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
            inputMethodManager.hideSoftInputFromWindow(token, InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }

    protected void showKeyboard(@Nullable View view) {
        if (view != null) {
            InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
            inputMethodManager.showSoftInput(view, 0);
        }
    }

    /**
     * 是否点击空白处收起键盘，true为点击收起
     *
     * @param autoCloseKeyboard
     */
    public void setAutoCloseKeyboard(boolean autoCloseKeyboard) {
        this.autoCloseKeyboard = autoCloseKeyboard;
    }

    protected FWActivity mActivity = this;


}
